import axios from 'axios';

/**
 * 用户行为记录混入，提供记录用户浏览、收藏、分享等行为的方法
 * 在需要记录用户行为的组件中使用: import userActionMixin from '@/mixins/userActionMixin';
 * 然后在setup函数中: const { recordAction } = userActionMixin();
 */
export default function userActionMixin() {
  /**
   * 记录用户行为
   * @param {number|string} goodsId - 商品ID 
   * @param {string} actionType - 行为类型: 'view'(浏览), 'collect'(收藏), 'share'(分享)
   * @returns {Promise} - 请求结果
   */
  const recordAction = async (goodsId, actionType) => {
    if (!goodsId || !actionType) {
      console.error('记录用户行为失败: 缺少必要参数');
      return;
    }
    
    try {
      const response = await axios.post('/api/recommend/action', {
        goodsId, 
        actionType
      });
      
      return response.data;
    } catch (error) {
      console.error('记录用户行为失败:', error);
    }
  };

  /**
   * 记录浏览行为
   * @param {number|string} goodsId - 商品ID
   */
  const recordView = async (goodsId) => {
    return await recordAction(goodsId, 'view');
  };

  /**
   * 记录收藏行为
   * @param {number|string} goodsId - 商品ID
   */
  const recordCollect = async (goodsId) => {
    return await recordAction(goodsId, 'collect');
  };

  /**
   * 记录分享行为
   * @param {number|string} goodsId - 商品ID
   */
  const recordShare = async (goodsId) => {
    return await recordAction(goodsId, 'share');
  };

  return {
    recordAction,
    recordView,
    recordCollect,
    recordShare
  };
} 